<template>
  <a-drawer
    title="新增住宅"
    :maskClosable="false"
    width=750
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="houseAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='住宅编码' v-bind="formItemLayout">
        <a-input v-decorator="['houseCode',
                   {rules: [
                    { required: true, message: '住宅编码不能为空'},
                    { max: 20, message: '长度不能超过20个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='住宅地址' v-bind="formItemLayout">
        <a-input v-decorator="['houseAdr']"/>
      </a-form-item>
      <a-form-item label='上级区域'
                   style="margin-bottom: 2rem"
                   v-bind="formItemLayout">
        <a-tree
          v-model="checkedKeys"
          ref="areaTree"
          :key="areaTreeKey"
          :checkable="true"
          :checkStrictly="true"
          @check="handleCheck"
          @expand="handleExpand"
          :expandedKeys="expandedKeys"
          :defaultCheckedKeys="defaultCheckedKeys"
          :treeData="areaTreeData">
        </a-tree>
      </a-form-item>

      <!--<a-form-item label='4级区域' v-bind="formItemLayout">
        <a-input-search v-decorator="['areaId']"
                  enter-button="搜索"
                  @search="showAreaSearch">
        </a-input-search>
      </a-form-item> -->
    </a-form>

    <div class="drawer-bootom-button">
      <a-dropdown style="float: left" :trigger="['click']" placement="topCenter">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="expandAll">展开所有</a-menu-item>
          <a-menu-item key="2" @click="closeAll">合并所有</a-menu-item>
        </a-menu>
        <a-button>
          树操作 <a-icon type="up" />
        </a-button>
      </a-dropdown>
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>

  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'HouseAdd',
  components: { },
  props: {
    houseAddVisiable: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      tHouse: {},
      searchType: '',
      rsSearch: {
        ownerVisible: false,
        ownerId: '',
        opeId: ''
      },
      areaSearch: {
        areaVisible: false
      },
      areaTreeKey: +new Date(),
      tArea: {},
      checkedKeys: [],
      checkedLevelNums: [],
      expandedKeys: [],
      areaTreeData: [],
      defaultCheckedKeys: []
    }
  },
  methods: {
    reset () {
      this.tHouse = {}
      this.loading = false
      this.areaTreeKey = +new Date()
      this.expandedKeys = this.checkedKeys = this.defaultCheckedKeys = []
      this.tArea = {}
      this.form.resetFields()
    },
    handleCheck (checkedKeys, info) {
      this.checkedKeys = checkedKeys
      info.checkedNodes.forEach(cn => {
          this.checkedLevelNums.push(cn.data.props.value)
      })
    },
    expandAll () {
      this.expandedKeys = this.allTreeKeys
    },
    closeAll () {
      this.expandedKeys = []
    },
    handleExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.setHouseFields()
          this.loading = true
          this.tHouse.areaId = this.checkedKeys.checked[0]
          this.$post('tHouse', {
            ...this.tHouse
          }).then(() => {
            this.reset()
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    setHouseFields () {
        let values = this.form.getFieldsValue(['houseCode', 'houseAdr', 'areaId'])
        if (typeof values !== 'undefined') {
            Object.keys(values).forEach(_key => { this.tHouse[_key] = values[_key] })
        }
    },
    handleRsSearchClose () {
      this.rsSearch.ownerVisible = false
      this.searchType = ''
    },
    handleRsSearchSuccess (rs, searchType) {
      console.log(rs)
      this.searchType = ''
      this.rsSearch.ownerVisible = false
      if (searchType === 'owner') {
        this.form.setFieldsValue({
          ownerName: rs.rsName || rs.cardId
        })
        this.rsSearch.ownerId = rs.id || ''
      } else if (searchType === 'opter') {
        this.form.setFieldsValue({
          opeName: rs.rsName || rs.cardId
        })
        this.rsSearch.opeId = rs.id || ''
      }
    },
    showOwnerSearch () {
      this.searchType = 'owner'
      this.rsSearch.ownerVisible = true
    },
    showOpeSearch () {
      this.searchType = 'opter'
      this.rsSearch.ownerVisible = true
    },
    showAreaSearch () {
      console.log('搜索片区')
    }
  },
  watch: {
    houseAddVisiable () {
      if (this.houseAddVisiable) {
        this.$get('tArea', {
        }).then((r) => {
          this.areaTreeData = r.data.rows.children
          this.allTreeKeys = r.data.ids
          this.areaTreeKey = +new Date()
        })
      }
    }
  }
}
</script>
